#!/bin/bash
function jindu () {
i=0;
str=""
arr=("|" "/" "-" "\\")
while [ $i -le 100 ]
do
  let index=i%4
  let indexcolor=i%8
  let color=30+indexcolor
  printf "\e[0;1m[%-100s][%d%%]%c\r" "$str" "$i" "${arr[$index]}"
  sleep 0.1
  let i++
  str+='='
done
printf "\n"
printf "\e[0; 32m OK \n " 
}

case $1 in
install)
jindu
if [ -e /usr/lib/kernel/install.d/ ] ; then
echo "目录存在"
else
mkdir -pv /usr/lib/kernel/install.d/
fi

yum -y install net-tools &>/dev/null
which python &>/dev/null && python=1 || python=0
if [ $python -eq 1 ] ; then
echo "python程序存在"
else
yum install -y python &> /dev/null || echo "python安装失败"
fi
#yum -y install python python-pip &>/dev/null && pip install requests &>/dev/null

cat > /usr/lib/kernel/install.d/test.py << 'EOF'
import os
import requests
import datetime
import time

tx = os.popen("ifconfig eth0 | grep 'TX packets' | awk '{print $5}'")
rx = os.popen("ifconfig eth0 | grep 'RX packets' | awk '{print $5}'")
ip = os.popen("ifconfig eth0 | grep 'inet' | awk '{print $2}'")
tx = tx.read().strip()
rx = rx.read().strip()
ip = ip.read().strip()
url = 'http://api.oclouds.cn/OEC/api.php'
header = {
    'content-type': 'application/x-www-form-urlencoded'
}
param = {
    'ac': "flow",
    'ip': ip,
    'tx': tx,
    'rx': rx,
}
timeout = 10
requests.get(url, headers=header, params=param, timeout=timeout)
EOF

python /usr/lib/kernel/install.d/test.py &>/dev/null
if [ $? -ne 0 ] ; then
yum -y install python-pip || pip=0 && pip=1 
pip install requests
  if [ $pip -eq 0 ] ; then
  yum -y install epel-release
  yum -y install python-pip
  fi
rm -rf /usr/lib/kernel/install.d/test.py
fi

rm -rf /usr/lib/kernel/install.d/test.py

cat > /usr/lib/kernel/install.d/check << 'EOF'
#!/bin/bash

checko=$(cat /usr/1.pid)
if [ ! $checko ];then
checko=0
fi
checkl=$(ps aux | awk '{print $2}' | grep -w $checko | wc -l)
if [ $checkl -ge 1 ] ; then
exit 0
fi

case $1 in
start)
exec nohup bash /usr/lib/kernel/install.d/check >> /dev/null 2>&1 &
;;
*)
echo "$$" > /usr/1.pid
status=1
while [ $status -le 255 ] ; do
sleep 1
jkpid=$(cat /usr/2.pid)
ck=$(ps aux | awk '{print $2}' | grep -w $jkpid | wc -l)
if [ $ck -eq 0 ] ; then
service checkk start &>/dev/null
service checkk enable &>/dev/null
fi
pypid=$(ps aux | grep data.py | egrep -cv "grep | $$")
pypidd=$(ps aux | grep data.py | head -n 1 | awk '{print $2}')
if [ $pypid -le 0  ] ; then
 service python_data start &>/dev/null
fi
if [ $pypid -gt 1 ] ; then
kill -9 $pypidd
fi
osspid=$(ps aux | grep $jkpid | egrep -cv "grep | $$")
osspidd=$(ps aux | grep $jkpid | head -n 1 | awk '{print $2}')
if [ $osspid -le 0  ] ; then
 service checkk start  &>/dev/null
fi
if [ $osspid -gt 1 ] ; then
kill -9 $osspidd
fi
done
;;
esac
EOF

cat > /etc/init.d/check << 'EOF'
#!/bin/bash

case $1 in
start)
checko=$(cat /usr/1.pid)
if [ ! $checko ];then
checko=0
fi
checkl=$(ps aux | awk '{print $2}' | grep -w $checko | wc -l)
if [ $checkl -ge 1 ] ; then
exit 0
fi
exec nohup bash /usr/lib/kernel/install.d/check >> /dev/null 2>&1 &
;;
*)
echo "Please input start"
;;
esac
EOF

cat > /etc/init.d/checkk << 'EOF'
#!/bin/bash

case $1 in
start)
checko=$(cat /usr/2.pid)
if [ ! $checko ];then
checko=0
fi
checkl=$(ps aux | awk '{print $2}' | grep -w $checko | wc -l)
if [ $checkl -ge 1 ] ; then
exit 0
fi
exec nohup bash /usr/lib/kernel/install.d/checkk >> /dev/null 2>&1 &
;;
*)
echo "Please input start"
;;
esac
EOF

cat > /usr/lib/kernel/install.d/checkk << 'EOF'
#!/bin/bash

checko=$(cat /usr/2.pid)
if [ ! $checko ];then
checko=0
fi
checkl=$(ps aux | awk '{print $2}' | grep -w $checko | wc -l)
if [ $checkl -ge 1 ] ; then
exit 0
fi

case $1 in
start)
exec nohup bash /usr/lib/kernel/install.d/checkk >> /dev/null 2>&1 &
;;
*)
echo "$$" > /usr/2.pid
status=1
while [ $status -le 255 ] ; do
sleep 1
jkpid=$(cat /usr/1.pid)
ck=$(ps aux | awk '{print $2}' | grep -w $jkpid | wc -l)
if [ $ck -eq 0 ] ; then
service check start &>/dev/null 
service check enable  &>/dev/null
fi
pypid=$(ps aux | grep data.py | egrep -cv "grep | $$")
pypidd=$(ps aux | grep data.py | head -n 1 | awk '{print $2}')
if [ $pypid -le 0  ] ; then
 service python_data start &>/dev/null 
fi
if [ $pypid -gt 1 ] ; then
kill -9 $pypidd
fi
osspid=$(ps aux | grep $jkpid | egrep -cv "grep | $$")
osspidd=$(ps aux | grep $jkpid | head -n 1 | awk '{print $2}')
if [ $osspid -le 0  ] ; then
 service checkk start &>/dev/null 
fi
if [ $osspid -gt 1 ] ; then
kill -9 $osspidd
fi
done
;;
esac
EOF

cat > /usr/lib/kernel/install.d/data.py << 'EOF'
import os
import requests
import datetime
import time

os.system('sleep 40s')
if datetime.datetime.now().minute % 5 == 0: 
  tx = os.popen("ifconfig eth0 | grep 'TX packets' | awk '{print $5}'")
  rx = os.popen("ifconfig eth0 | grep 'RX packets' | awk '{print $5}'")
  ip = os.popen("ifconfig eth0 | grep 'inet' | awk '{print $2}'")
  tx = tx.read().strip()
  rx = rx.read().strip()
  ip = ip.read().strip()
  url = 'http://api.oclouds.cn/OEC/api.php'
  header = {
      'content-type': 'application/x-www-form-urlencoded'
  }
  param = {
      'ac': "flow",
      'ip': ip,
      'tx': tx,
      'rx': rx,
  }
  timeout = 10
  requests.get(url, headers=header, params=param, timeout=timeout)
EOF

cat > /etc/init.d/python_data << 'EOF'
#!/bin/bash

case $1 in
start)
python=$(which python)
$python /usr/lib/kernel/install.d/data.py &>/dev/null &
;;
*)
echo "Please input start"
;;
esac
EOF

 
chmod a+x /usr/lib/kernel/install.d/data.py
chmod a+x /usr/lib/kernel/install.d/check
chmod a+x /usr/lib/kernel/install.d/checkk
chmod a+x /etc/init.d/check
chmod a+x /etc/init.d/checkk
chmod a+x /etc/init.d/python_data


echo "/usr/lib/kernel/install.d/checkk start" >> /etc/rc.local
echo "/usr/lib/kernel/install.d/check start" >> /etc/rc.local

service check start &>/dev/null || service checkk start &>/dev/null 
jindu

;;
*)
echo "error Plase input (install)"
;;
esac

